/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.himilco.ocana.gui;

import com.himilco.ocana.workflow.core.dao.entities.Ticket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author oussema
 */
public class LazyTicketDataModel extends LazyDataModel<Ticket> {

    private List<Ticket> datasource;

    public LazyTicketDataModel(List<Ticket> datasource) {
        this.datasource = datasource;
    }

    @Override
    public Ticket getRowData(String rowKey) {
        for (Ticket ticket : datasource) {
//            if (Long.toString(ticket.getIdTicket()).equals(rowKey)) {
            if (ticket.getStatus().equals(rowKey)) {
                return ticket;
            }
        }
        return null;
    }

    public Object getRowKey(Ticket ticket) {
        return ticket.getStatus();
    }

    public List<Ticket> getDatasource() {
        return datasource;
    }

    public void setDatasource(List<Ticket> datasource) {
        this.datasource = datasource;
    }

    public void setRowIndex(int rowIndex) {
        /*
         * The following is in ancestor (LazyDataModel): this.rowIndex =
         * rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
         */
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        } else {
            super.setRowIndex(rowIndex % getPageSize());
        }
    }

    @Override
    public List<Ticket> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        List<Ticket> data = new ArrayList<Ticket>();

        //filter  
        for (Ticket ticket : datasource) {
            boolean match = true;

            for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                try {
                    String filterProperty = it.next();
                    String filterValue = filters.get(filterProperty);
                    String fieldValue = String.valueOf(ticket.getClass().getField(filterProperty).get(ticket));
                    //String fieldValue = String.valueOf(ticket.getClass().getField(filterProperty).get(ticket));
                    if (filterValue == null || fieldValue.startsWith(filterValue)) {
                        match = true;
                    } else {
                        match = false;
                        break;
                    }
                } catch (Exception e) {
                    match = false;
                }
            }

            if (match) {
                data.add(ticket);
            }
        }

        //sort  
        if (sortField != null) {
//            Collections.sort(data, new LazySorter(sortField, sortOrder));  
        }

        //rowCount  
        int dataSize = data.size();
        this.setRowCount(dataSize);

        //paginate  
        if (dataSize > pageSize) {
            try {
                return data.subList(first, first + pageSize);
            } catch (IndexOutOfBoundsException e) {
                return data.subList(first, first + (dataSize % pageSize));
            }
        } else {
            return data;
        }
    }
}
